﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using VisualWip.PresentationModel.WorkItem;

namespace VisualWip.PresentationModel
{
    public class WorkItemTypeTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            var workItem = item as WorkItemViewModel;
            if (workItem != null)
            {
                string resourceKey = String.Format(CultureInfo.InvariantCulture, "{0}_{1}_WorkItemView", workItem.TypeName, workItem.Size);
                var template = Application.Current.TryFindResource(resourceKey) as DataTemplate;
                if (template != null)
                {
                    return template;
                }

                var defaultResourceKey = string.Format("DefaultType_{0}_WorkItemView", workItem.Size);

                var defaultTypeTemplate = Application.Current.TryFindResource(defaultResourceKey) as DataTemplate;
                if (defaultTypeTemplate != null)
                {
                    return defaultTypeTemplate;
                }

                var defaultTypeAndSizeTemplate = Application.Current.TryFindResource("DefaultType_Large_WorkItemView") as DataTemplate;
                if (defaultTypeAndSizeTemplate != null)
                {
                    return defaultTypeAndSizeTemplate;
                }


            }

            return base.SelectTemplate(item, container);
        }
    }
}